#!/usr/bin/env python3

"""
@author: DDD
@file: manage.py
@time: 2022/5/13 17:53
"""

from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
manage = Manager(app)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:yzxa1234.@124.221.93.136:3308/flask'

# 动态追踪修改设置，如未设置只会提示警告
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# 查询时会显示原始SQL语句
app.config['SQLALCHEMY_E CHO'] = True

db = SQLAlchemy(app)
# postgresql://scott:tiger@localhost/mydatabase
# - oracle://scott:tiger@127.0.0.1:1521/sidname

class Role(db.Model):
    # 定义表名
    __tablename__ = 'roles'
    # 定义列对象
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    us = db.relationship('User', backref='role')

    #repr()方法显示一个可读字符串
    def __repr__(self):
        return 'Role:%s'% self.name

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True, index=True)
    password = db.Column(db.String(64))
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return 'User:%s'%self.name

@app.route('/')
def index():
    db.create_all()
    ro1 = Role(name='admins')
    db.session.add(ro1)
    db.session.commit()

    # ro2 =  Role(name='xishi')
    # db.session.add(ro2)
    # db.session.commit()
    # us1 = User(name='wang', password='123456', role_id=ro1.id)
    # us2 = User(name='zhang', password='201512', role_id=ro2.id)
    # us3 = User(name='chen', password='987654', role_id=ro2.id)
    # us4 = User(name='zhou', password='456789', role_id=ro1.id)
    # us5 = User(name='tang', password='158104', role_id=ro2.id)
    # us6 = User(name='wu', password='5623514', role_id=ro2.id)
    # us7 = User(name='qian', password='1543567', role_id=ro1.id)
    # us8 = User(name='liu', password='867322', role_id=ro1.id)
    # us9 = User(name='li', password='4526342', role_id=ro2.id)
    # us10 = User(name='sun', password='235523', role_id=ro2.id)
    # db.session.add_all([us1, us2, us3, us4, us5, us6, us7, us8, us9, us10])
    # db.session.commit()


    return 'hello'

if __name__ == '__main__':
    # manage.run()
    app.run(port=8888)
